package com.delaphone.fadmin.action;

import com.delaphone.fadmin.service.CustomerService;
import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.util.StringUtil;
import com.delaphone.flocash.web.bean.SelectList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Manage Customer Account
 * @author dongtv
 */
public class ManageCustomerAction extends CoreAction {

    private String email;
    private String startDateCreated;
    private String endDateCreated;
    private String firstName;
    private String lastName;
    private String city;
    private String country;
    private CustomerService customerService;
    private List<FUser> customers;
    private int pageNumber = 1;
    private int pageSize = 4;
    private long totalPages;
    private ArrayList<SelectList> pages;

    public String index() {
        Date now = new Date();
        Date startD, endD;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (startDateCreated == null || startDateCreated.isEmpty()) {

            startD = StringUtil.addDate(now, -900);// 1 month ago
            startDateCreated = StringUtil.formatDisplayDate(startD);
        } else {
            try {
                startD = formatter.parse(startDateCreated);
            } catch (Exception ex) {
                startD = StringUtil.addDate(now, -900);// 1 month ago
                startDateCreated = StringUtil.formatDisplayDate(startD);
            }
        }

        if (endDateCreated == null || endDateCreated.isEmpty()) {

            endD = StringUtil.addDate(now, -900);// 1 month ago
            endDateCreated = StringUtil.formatDisplayDate(endD);
        } else {
            try {
                endD = formatter.parse(endDateCreated);
            } catch (Exception ex) {
                endD = StringUtil.addDate(now, 1);// 1 month ago
                endDateCreated = StringUtil.formatDisplayDate(endD);
            }

        }
        customers = customerService.findCustomers(startD, endD, email, firstName, lastName, city, country, pageSize, pageNumber);
        long total = customerService.findCustomersRowCount(startD, endD, email, firstName, lastName, city, country);
        if (total == 0) {
            totalPages = 1;
        } else {
            totalPages = ((total - 1) / pageSize) + 1;
        }

//        logger.info("total:" + total);
//        logger.info("email:" + email);
//        logger.info("firstname:" + firstName);
//        logger.info("lastname:" + lastName);
//        logger.info("city:" + city);
//        logger.info("totalpages:" + totalPages);
//        logger.info("size:" + customers.size());
        return SUCCESS;
    }

    @Override
    public String execute() {
        Date now = new Date();
        Date startD, endD;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (startDateCreated == null || startDateCreated.isEmpty()) {

            startD = StringUtil.addDate(now, -900);// 1 month ago
            startDateCreated = StringUtil.formatDisplayDate(startD);
        } else {
            try {
                startD = formatter.parse(startDateCreated);
            } catch (Exception ex) {
                startD = StringUtil.addDate(now, -900);// 1 month ago
                startDateCreated = StringUtil.formatDisplayDate(startD);
            }
        }

        if (endDateCreated == null || endDateCreated.isEmpty()) {

            endD = StringUtil.addDate(now, 1);// 1 month ago
            endDateCreated = StringUtil.formatDisplayDate(endD);
        } else {
            try {
                endD = formatter.parse(endDateCreated);
            } catch (Exception ex) {
                endD = StringUtil.addDate(now, 1);// 1 month ago
                endDateCreated = StringUtil.formatDisplayDate(endD);
            }

        }
        
        customers = customerService.findCustomers(startD, endD, email, firstName, lastName, city, country, pageSize, pageNumber);
        long total = customerService.findCustomersRowCount(startD, endD, email, firstName, lastName, city, country);
        if (total == 0) {
            totalPages = 1;
        } else {
            totalPages = ((total - 1) / pageSize) + 1;
        }

//        logger.info("total:" + total);
//        logger.info("email:" + email);
//        logger.info("firstname:" + firstName);
//        logger.info("lastname:" + lastName);
//        logger.info("city:" + city);
//        logger.info("totalpages:" + totalPages);
//        logger.info("size:" + customers.size());
        return SUCCESS;
    }

    
    public String update() {
        return SUCCESS;
    }

    public String sendMail() {
        return SUCCESS;
    }
// <editor-fold defaultstate="collapsed" desc="get and set methods">

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public ArrayList<SelectList> getPages() {
        pages = new ArrayList<SelectList>();
        for (long i = 1; i <= totalPages; i++) {
            String id = String.valueOf(i);
            // String value="@<a href=/opt/doSearchCustomer.htm>"+id+"</a>";
            pages.add(new SelectList(id, id));

        }

        return pages;
    }

    public void setPages(ArrayList<SelectList> pages) {
        this.pages = pages;
    }

    public String getEndDateCreated() {
        return endDateCreated;
    }

    public void setEndDateCreated(String endDateCreated) {
        this.endDateCreated = endDateCreated;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public List<FUser> getCustomers() {
        return customers;
    }

    public void setCustomers(List<FUser> customers) {
        this.customers = customers;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getStartDateCreated() {
        return startDateCreated;
    }

    public void setStartDateCreated(String startDateCreated) {
        this.startDateCreated = startDateCreated;
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
    // </editor-fold>
}
